package com.lgd.experiment;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by 299174105 on 2017/9/8.
 */
public class JavaTestBonc {
    public static void main(String[] args) {
        /*int x=0,y=0,z=0;
        try{
            z=x/y;
        }catch(Exception e){
            z=1;
        }finally{
            z=2;
        }
        System.out.println(z);
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(new Date());
        System.out.println(format);
        int a[ ]=new int[8];
        System.out.println(a.length);

        Map<String ,String> dd = new HashMap<>();
        dd.put("userid","123456");
        dd.put("areaid","431");
        dd.keySet().forEach(System.out::println);
        dd.values().forEach(System.out::println);*/

      /*  for(int i = 1;i<10;i++){
            for (int j = 1; j <= i; j++) {
                System.out.print(j+"*"+i+"="+i*j+" ");
                if (i==j){
                    System.out.println();
                }
            }
        }*/
        File a = new File("D:\\FILE\\a.txt");
        File b = new File("D:\\FILE\\b.txt");
        File c = new File("D:\\FILE\\c.txt");
        JavaTestBonc t = new JavaTestBonc();
        try {
            t.connectWords(a, b, c);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public  void connectWords(File file1, File file2, File file3)throws IOException
    {
        String[] str1 = split(file1, "\n");
        String[] str2 = split(file2, "\n"+"|"+" ");
        try(FileWriter fw = new FileWriter(file3))
        {
            int index = 0;
            while(index <= str1.length||index <= str2.length)
            {
                if(index < str1.length)
                    fw.write("\n"+str1[index]+"\n");
                if(index < str2.length)
                    fw.write("\n"+str2[index]+"\n");
                index ++;
            }
        }
    }
    public String[] split(File f, String regex)throws IOException
    {
        try(FileReader fr = new FileReader(f))
        {
            char[] cbuf = new char[(int)f.length()];
            int hasRead = fr.read(cbuf);
            String str = new String(cbuf, 0, hasRead);
            String[] strArr = str.split(regex);
            return strArr;
        }
    }
}
